﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Data.SqlTypes;
using AutoMapper;

namespace Common
{
    /// <summary>
    /// ConvertUtils class
    /// </summary>
    /// <author>Tien</author>
    public class ConvertUtils
    {
        public static string ToString(string Value)
        {
            return (Value != null) ? Value.Trim() : String.Empty;
        }

        public static string ToString(Object Value)
        {
            return (Value != null) ? Value.ToString() : String.Empty;
        }

        public static double ToDouble(object value)
        {
            if (value is DBNull)
                return 0;
            return Convert.ToDouble(value);
        }

        public static int ToInt32(object value)
        {
            if (value is DBNull)
                return 0;
            return Convert.ToInt32(value);
        }

        public static long ToLong(object value)
        {
            if (value is DBNull)
                return 0;
            return Convert.ToInt64(value);
        }

        public static DateTime? ToDateTime(Object value)
        {
            if (value != null && value.ToString() != String.Empty)
                return Convert.ToDateTime(value);
            return null;
        }

        public static bool ToBoolean(Object value)
        {
            if (value is DBNull)
                return false;
            return Convert.ToBoolean(value);
        }


        //Ham chuyen doi ve chuoi khong dau
        public static string ConvertToUnSign(string text)
        {
            text = text.Trim();
            for (int i = 33; i < 48; i++)
            {
                text = text.Replace(((char)i).ToString(), "");
            }

            /*for (int i = 58; i < 65; i++)
            {
                text = text.Replace(((char)i).ToString(), "");
            }*/

            //ASCII ":" 58
            for (int i = 58; i < 65; i++)
            {
                text = text.Replace(((char)i).ToString(), "");
            }

            for (int i = 91; i < 97; i++)
            {
                text = text.Replace(((char)i).ToString(), "");
            }

            for (int i = 123; i < 127; i++)
            {
                text = text.Replace(((char)i).ToString(), "");
            }

            //text = text.Replace(" ", "-"); //Comment l?i d? không dua kho?ng tr?ng thành ký t? -

            Regex regex = new Regex(@"\p{IsCombiningDiacriticalMarks}+");
            string strFormD = text.Normalize(System.Text.NormalizationForm.FormD);
            return regex.Replace(strFormD, String.Empty).Replace('\u0111', 'd').Replace('\u0110', 'D');
        }

        public static string ConvertDatetimeToStringSQLDatetime(DateTime datetime)
        {
            return datetime.ToString("yyyy-MM-dd");
        }

        //Ham chuyen doi chuoi dinh dang sang ngay dd/MM/yyyy
        public static DateTime ConvertStringToShortDate(string pDatetimeStr)
        {
            try
            {
                pDatetimeStr = pDatetimeStr.Trim();
                if (pDatetimeStr != "")
                {
                    IFormatProvider theCultureInfo = new System.Globalization.CultureInfo("en-GB", true);
                    DateTime result = DateTime.ParseExact(pDatetimeStr, "dd/MM/yyyy", theCultureInfo);
                    return result;
                }
                else
                    return SqlDateTime.MinValue.Value;
            }
            catch
            {
                return DateTime.Now.Date;
            }
        }

        //Ham chuyen doi chuoi dinh dang sang ngay dd/MM/yyyy hh:mm:ss
        public static DateTime ConvertStringToLongDate(string pDatetimeStr)
        {
            try
            {
                pDatetimeStr = pDatetimeStr.Trim();
                if (pDatetimeStr != "")
                {
                    IFormatProvider theCultureInfo = new System.Globalization.CultureInfo("en-GB", true);
                    DateTime result = DateTime.ParseExact(pDatetimeStr, "dd/MM/yyyy HH:mm:ss", theCultureInfo);
                    return result;
                }
                else
                    return SqlDateTime.MinValue.Value;
            }
            catch
            {
                return DateTime.Now;
            }
        }

        public static IFormatProvider DatetimeFormatCustom()
        {
            System.Globalization.CultureInfo tem = new System.Globalization.CultureInfo("en-GB", true);
            tem.DateTimeFormat.FullDateTimePattern = "dd/MM/yyy hh:mm:ss";
            return tem;
        }

        public static DateTime ReturnDateTimeFromNo(int No, DateTime startDate, DateTime endDate)
        {
            int index = 1;
            while (startDate <= endDate)
            {
                if (index == No)
                {
                    break;
                }
                startDate = startDate.AddDays(1);
                index++;
            }
            return startDate;
        }
    }

    public class MappingObject<S, D>
    {
        public static void MapperObject(ref S objectA, ref D objectB)
        {
            Mapper.CreateMap<S, D>();
            objectB = Mapper.Map<S, D>(objectA);
        }
    }
}
